package net.jalbum.filters;

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

public class ResizableObjectPanel extends BasicFilter {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2109546799471864726L;
	protected ResizableObject resizableObject;
	protected ResizableObjectControl resizableObjectControl;
	protected double X1;
	protected double Y1;
	protected double X2;
	protected double Y2;
	protected boolean showOutline = true;

	public ResizableObjectPanel() {
		this.name = "ResizableObjectPanel";
		this.author = "David Fichtmueller";
		this.version = "1.0";
		this.description = "This is just for inheriting for other filters. This is not applicable.";
		this.prescale = false;
		this.postscale = false;

		this.resizableObjectControl = new ResizableObjectControl(this);
		this.resizableObject = new ResizableObject();
		this.resizableObject.setParentPanel(this);
	}

	@Override
	public void setPreviewImage(final BufferedImage bi) {
		this.width = bi.getWidth();
		this.height = bi.getHeight();
		this.resizableObject.setWidth(this.width);
		this.resizableObject.setHeight(this.height);
		this.resizableObjectControl.setObjectWidth(this.width);
		this.resizableObjectControl.setObjectHeight(this.height);
		super.setPreviewImage(bi);
	}

	@Override
	public void paint(final Graphics g) {
		// final image
		g.drawImage(this.outputImage, 0, 0, null);
		if (this.showOutline) {
			this.resizableObject.paint(g);
		}
	}

	public void setCursor(final Cursor cursor) {
		this.filterManager.setCursor(cursor);
	}

	public Cursor getCursor() {
		return this.filterManager.getCursor();
	}

	public void updateController(final double x1, final double y1, final double x2, final double y2) {
		this.X1 = x1;
		this.Y1 = y1;
		this.X2 = x2;
		this.Y2 = y2;
		this.resizableObjectControl.update(x1, y1, x2, y2);
		this.renderPreview();
	}

	public void updateObject(final double x1, final double y1, final double x2, final double y2) {
		// this method is called by the controller to pass new settings to the object
		this.X1 = x1;
		this.Y1 = y1;
		this.X2 = x2;
		this.Y2 = y2;
		this.resizableObject.update(x1, y1, x2, y2);
		this.resizableObject.setInitial(false);
		this.renderPreview();
	}

	@Override
	public void keyPressed(final KeyEvent arg0) {
            System.out.println(arg0);
		this.resizableObject.keyPressed(arg0);
	}

	@Override
	public void keyReleased(final KeyEvent arg0) {
		this.resizableObject.keyReleased(arg0);
	}

	@Override
	public void keyTyped(final KeyEvent arg0) {
		this.resizableObject.keyTyped(arg0);
	}

	@Override
	public void mouseClicked(final MouseEvent event) {
		this.resizableObject.mouseClicked(event);
	}

	@Override
	public void mouseMoved(final MouseEvent event) {
		this.resizableObject.mouseMoved(event);
	}

	@Override
	public void mouseDragged(final MouseEvent event) {
		this.resizableObject.mouseDragged(event);
	}

	@Override
	public void mouseEntered(final MouseEvent event) {
		this.resizableObject.mouseEntered(event);
	}

	@Override
	public void mouseExited(final MouseEvent event) {
		this.resizableObject.mouseExited(event);
	}

	@Override
	public void mousePressed(final MouseEvent event) {
		this.resizableObject.mousePressed(event);
	}

	@Override
	public void mouseReleased(final MouseEvent event) {
		this.resizableObject.mouseReleased(event);
	}

	public boolean isShowOutline() {
		return this.showOutline;
	}

	public void setShowOutline(final boolean showOutline) {
		this.showOutline = showOutline;
	}

	public double getX1() {
		return this.X1;
	}

	public void setX1(final double x1) {
		this.X1 = x1;
	}

	public double getY1() {
		return this.Y1;
	}

	public void setY1(final double y1) {
		this.Y1 = y1;
	}

	public double getX2() {
		return this.X2;
	}

	public void setX2(final double x2) {
		this.X2 = x2;
	}

	public double getY2() {
		return this.Y2;
	}

	public void setY2(final double y2) {
		this.Y2 = y2;
	}

	public ResizableObject getResizableObject() {
		return this.resizableObject;
	}

	public void setResizableObject(final ResizableObject resizableObject) {
		this.resizableObject = resizableObject;
	}
}